﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using BusinessSite.Controls;
using BusinessSite.Utility;
using BusinessSite;
using Business;

using Db4o.Framework.Business;
using Db4o.Framework.Query;
using Db4o.Framework;



namespace BusinessSite
{
    public partial class AddProduct : System.Web.UI.UserControl
    {
        private ErrorType connexionStatus;

        public ErrorType ConnexionStatus
        {
            get { return connexionStatus; }
            set { connexionStatus = value; }
        }

        private Product current;
        public Product Current
        {
            get { return current; }
            set { current = value; }
        }

        private int productId;
        public int ProductId
        {
            get { return productId; }
            set { productId = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            ddlProducts.Attributes.Add("onchange", "submitRequestProduct();");

            if (BusinessControl.Instance.Connected)
            {
                if (Request.Params["id"] != String.Empty) ProductId = Int32.Parse(Request.Params["id"]);

                if (IsPostBack)
                {
                    BusinessControl.Instance.StoreObject(Request, typeof(Business.Product),ProductId);
                }
                else
                {
                    ConnexionStatus = ErrorType.Connected;
                    IList<Product> products = DataManager.Instance.Retreive<Product>();

                    ddlProducts.DataSource = products;
                    ddlProducts.DataTextField = "ProductName";
                    ddlProducts.DataValueField = "Id";
                    ddlProducts.DataBind();
                    ddlProducts.Items.Insert(0, new ListItem("Select an item","0"));
                    ddlProducts.SelectedIndex = 0;

                    if (ProductId > 0)
                    {
                        Current = DataManager.Instance.RetreiveById<Product>(ProductId);
                        FillProduct(Current);
                    }
                    else if (products.Count > 0)
                    {
                       // Current = products[0];

                        //FillProduct(Current);
                    }
                }
            }
            else
            {
                ConnexionStatus = ErrorType.NotConnected;
                Utility.WebPartsRenderer.Instance.HtmlErrorMessage((Literal)Page.Master.FindControl("LiteralErrorMessage"), ConnexionStatus);
            }
        }
        public void FillProduct(Product product)
        {
            if (product != null)
            {
                SelectedProduct(product.Id);

                txtProductId.Text = product.Id.ToString();
                txtProductName.Text = product.ProductName;
                txtQuantityPerUnit.Text = product.QuantityPerUnit.ToString();
                txtReorderLevel.Text = product.ReorderLevel.ToString();
                txtUnitPrice.Text = product.UnitPrice.ToString();
                txtUnitsInStock.Text = product.UnitInStock.ToString();
                txtUnitsOnOrder.Text = product.UnitsOnOrder.ToString();
            }
        }
        private void SelectedProduct(long id)
        {
            foreach (ListItem item in ddlProducts.Items)
                if (item.Value == id.ToString())
                    ddlProducts.SelectedIndex = ddlProducts.Items.IndexOf(item);
        }


    }
}
